Ninja Database Pro
Object Children vs. Relational Children
Basic Tasks > Object Children vs. Relational Children

Ninja Database Pro can save child or parent objects separately or embedded with object being saved .  This is known as either relational or object mode.  To save child or parent objects as separate tables, decorate the child object with the ForeignKey attribute.  See Setting up Relationships.  With no attributes the object and its children are saved together as a single item in the database.  You can have a mixture of either type in your class.  However, once set the relationship cannot be changed. 

 

Here is an example of an object with children that will be saved together.

 

    public class Store

    {

        //This is the primary key

        public long StoreId { get; set; }

 

        //Normal table columns

        public string Name { get; set; }

        public string City { get; set; }

        public string State { get; set; }

 

        //This has no relationship defined so it will be stored on the object

        public List<Department> Departments { get; set; }

    }

 

    public class Department

    {

        public string DepartmentName { get; set; }

    }

 

 

NinjaDbPro db = new NinjaDbPro("MyDatabaseDirectory", "MyDatabaseName");

 

//Licensed Mode

//db.UserName = "John Smith 101224";

//db.LicenseKey = "aousdf832jasf==";

 

//Set before OpenDatabase. Default storage is IsolatedStorageDatabase. Other options are:

//db.Storage = new MemoryDatabase(); //In memory database

//db.Storage = new FileDatabase(); //Valid only for non Silverlight projects

 

db.OpenDatabase();

 

Store store = new Store();

store.Name = "Spatula City";

store.City = "Lynwood";

store.State = "CA";

 

store.Departments = new List<Department>();

store.Departments.Add(new Department{DepartmentName = "Metal Spatulas"});

store.Departments.Add(new Department { DepartmentName = "Wide Spatulas" });

store.Departments.Add(new Department { DepartmentName = "Gift Spatulas" });

 

db.Save(store);

 

Store storeCopy = db.Load<Store>(store.StoreId);

 

db.CloseDatabase();